package com.luxsoft.siipap.em.replica.ui;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.jdesktop.swingx.JXTable;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.impl.matchers.FixedMatcherEditor;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.matchers.ThreadedMatcherEditor;
import ca.odell.glazedlists.swing.EventTableModel;

import com.luxsoft.siipap.em.replica.model.Registro;
import com.luxsoft.siipap.swing.utils.ComponentUtils;

/**
 * Muestra en un grid una lista filtrada de instancias de
 * {@link Registro} pero unicamente para ARSALD 
 * 
 * @author Ruben Cancino
 *
 */
public class RegistrosDeSaldosPanel extends JPanel{
	
	private final EventList<Registro> source;
	private SortedList<Registro> sortedSaldos;
	private JXTable grid;
	
	public RegistrosDeSaldosPanel(final EventList<Registro> regs){
		this.source=regs;
	}
	
	private void initGlazedLists(){
		Matcher<Registro> matcher=new Matcher<Registro>(){

			public boolean matches(Registro item) {
				return item.getArchivo().startsWith("ARSALD");
			}
			
		};
		FixedMatcherEditor<Registro> editor=new FixedMatcherEditor<Registro>(matcher);
		FilterList<Registro> filterList=new FilterList<Registro>(source, new ThreadedMatcherEditor<Registro>(editor));
		sortedSaldos=new SortedList<Registro>(filterList,null);
	}

	@Override
	public void addNotify() {
		initComponents();
		super.addNotify();
	}
	
	private JSplitPane sp;
	
	private void initComponents(){
		initGlazedLists();
		setLayout(new BorderLayout());
		sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		
		// Registros grid
		TableFormat<Registro> tf=GlazedLists.tableFormat(Registro.class
			,new String[]{"archivo","tipo","llave","saldoDeArticulo","sucursalArsald","productoArsald","origen"}
			,new String[]{"archivo","tipo","llave","saldoDeArticulo","sucursalArsald","productoArsald","origen"}
		);
		EventTableModel<Registro> tm=new EventTableModel<Registro>(sortedSaldos,tf);
		grid=ComponentUtils.getStandardTable();
		grid.setModel(tm);
		sp.setLeftComponent(new JScrollPane(grid));
		
		add(sp,BorderLayout.CENTER);
	}
	

}
